package com.huel.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class GetUtils {
    // 备用Ip地址 机房1 地址10.251.123.47
    // 重新修改 URL 地址  192.168.199.229 是 學校的ip 地址

    public static String LOGIN_URL = "http://192.168.199.1:8080/Android_war_exploded/login?";

    public static String REG_URL = "http://192.168.199.1:8080/Android_war_exploded/register?";

    public static String  ADD_URL= "http://192.168.199.1:8080/Android_war_exploded/addStudent?";

    public static String  FIND_URL= "http://192.168.199.1:8080/Android_war_exploded/findStudent?";


    public  static  String DEL_URL ="http://192.168.199.1:8080/Android_war_exploded/delStudent?";
    public static String XINXIANG = "101180301";
    public static String KEY = "c8d8f054840442d0a7d4ac27c1bc300c";

    public static String CITYAPI = "https://geoapi.heweather.net/v2/city/lookup?location=";

    public static String WEATHERAPI = "https://devapi.heweather.net/v7/weather/now?location=101180301&key=c8d8f054840442d0a7d4ac27c1bc300c";  //新乡天气
//    public static String StaffId = "1000000";
//    public static String AppSecret = "7944616D0CA1095B39D8A97166975D19D3727B5200924F68B5EC28E8AA3427F8";

    public static  String THREEDAY="https://devapi.heweather.net/v7/weather/3d?location=101180301&key=c8d8f054840442d0a7d4ac27c1bc300c";  //新乡三天天气
    private String name, password;
    public GetUtils(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public static String getWeatherData() {
        String weatherData = null;
        String path = WEATHERAPI;
        try {
            URL url = new URL("http://192.168.199.229:8080/Android_war_exploded/cakeServlet?action=list");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;

            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("请求服务器成功");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                weatherData = reader.readLine();


            }
            //关闭流和连接
            reader.close();
            inputStream.close();
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return weatherData;
    }



    public static String findByUtils(String information){
        String  msg ="";

        try{
            System.out.println("请求的数据是:"+information);
            //我们请求的数据:  根据 学号删除 信息 因为 学号 是主键  保证 是 唯一性
            String data = "information=" + URLEncoder.encode(information, "UTF-8") ;
            //get请求的url
            URL url = new URL(FIND_URL + data);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;
            //如果应答码为200的时候，表示成功的请求带了，这里的HttpURLConnection.HTTP_OK就是200
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("请求搜索服务成功");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                msg = reader.readLine();
                //关闭流和连接
                reader.close();
                inputStream.close();
                conn.disconnect();
            }else {
                System.out.println("服务器未响应");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return msg;
    }

    public static String delByUtils(String sno){
        String  msg ="";
        try{
            System.out.println("请求的数据是:"+sno);
            //我们请求的数据:  根据 学号删除 信息 因为 学号 是主键  保证 是 唯一性
            String data = "sno=" + URLEncoder.encode(sno, "UTF-8") ;
            //get请求的url
            URL url = new URL(DEL_URL + data);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;
            //如果应答码为200的时候，表示成功的请求带了，这里的HttpURLConnection.HTTP_OK就是200
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("请求删除服务器成功");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                msg = reader.readLine();
                System.out.println("delServelt:--->"+msg);
                //关闭流和连接
                reader.close();
                inputStream.close();
                conn.disconnect();
            }else {
                System.out.println("服务器未响应");
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return  msg;
    }

    public static String regByUtils(String name, String password) {
        String msg = "";
        try {
            //我们请求的数据:
            String data = "name=" + URLEncoder.encode(name, "UTF-8") +
                    "&password=" + URLEncoder.encode(password, "UTF-8");
            //get请求的url
            URL url = new URL(REG_URL + data);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;
            //如果应答码为200的时候，表示成功的请求带了，这里的HttpURLConnection.HTTP_OK就是200
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("请求服务器成功");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                msg = reader.readLine();
                System.out.println(msg);
            }
            //关闭流和连接
            reader.close();
            inputStream.close();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }




    public static String getThreeDays(){
        String weatherData = null;

        String path = THREEDAY;
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;

            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("三天信息请求完成");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                weatherData = reader.readLine();


            }
            //关闭流和连接
            reader.close();
            inputStream.close();
            conn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return weatherData;
    }



    public static String QueryInfo() {
        System.out.println("正在请求服务器。。。。");
        String msg = "";
        try {

            //get请求的url
            URL url = new URL("http://192.168.199.229:8080/Android_war_exploded/query");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;
            //如果应答码为200的时候，表示成功的请求带了，这里的HttpURLConnection.HTTP_OK就是200
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("请求服务器成功");
                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                msg = reader.readLine();
                System.out.println(msg);
            }
            //关闭流和连接
            reader.close();
            inputStream.close();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }


    public static String addInfo(String sno,String name,String classNum){

        System.out.println("正在请求服务器。。。。");
        String msg = "";
        try {
            //get请求的url

            System.out.println("sno="+sno+"name="+name+"classNum="+classNum);

            //我们请求的数据:
            String data = "sno=" + URLEncoder.encode(sno, "UTF-8") +
                    "&name=" + URLEncoder.encode(name, "UTF-8")+ "&classNum=" + URLEncoder.encode(classNum, "UTF-8");
            URL url = new URL(ADD_URL+data);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置请求方式,请求超时信息
            conn.setRequestMethod("GET");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            //开启连接
            conn.connect();
            InputStream inputStream = null;
            BufferedReader reader = null;
            //如果应答码为200的时候，表示成功的请求带了，这里的HttpURLConnection.HTTP_OK就是200
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

                //获得连接的输入流
                inputStream = conn.getInputStream();
                //转换成一个加强型的buffered流
                reader = new BufferedReader(new InputStreamReader(inputStream));
                //把读到的内容赋值给result
                msg = reader.readLine();
                System.out.println(msg);
            }
            //关闭流和连接
            reader.close();
            inputStream.close();
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;


    }
}
